package com.qs.controller;

import com.qs.controller.vo.RoleVO;
import com.qs.mapper.entity.Role;
import com.qs.service.RoleService;
import com.qs.service.bo.RoleBO;
import com.qs.util.BeanCopyUtils;
import com.qs.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/role")
@RequiredArgsConstructor
public class RoleController {
    private final RoleService roleService;

    @GetMapping
    private R<List<RoleBO>> list() {
        List<Role> roleList = roleService.list();
        List<RoleBO> roleBOList = BeanCopyUtils.copyBeanList(roleList, RoleBO.class);
        return R.ok(roleBOList);
    }

    @PostMapping
    private R<String> add(@RequestBody RoleVO roleVO) {
        RoleBO roleBO = BeanCopyUtils.copyBean(roleVO, RoleBO.class);
        String result = roleService.add(roleBO);
        return R.ok(result);
    }

    @PutMapping
    private R<String> edit(@RequestBody RoleVO roleVO) {
        RoleBO roleBO = BeanCopyUtils.copyBean(roleVO, RoleBO.class);
        String result = roleService.edit(roleBO);
        return R.ok(result);
    }

    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable("id") Long id) {
        boolean removed = roleService.removeById(id);
        return R.ok(removed);
    }
}
